
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
    int res = 0;

public:
    void dfs(TreeNode *root, int max)
    {
        if (!root)
            return;
        if (root->val >= max)
            max = root->val, res++;
        dfs(root->left, max);
        dfs(root->right, max);
    }
    int goodNodes(TreeNode *root)
    {
        dfs(root, root->val);
        return res;
    }
};